package com.lry.pda.bottom.c.backend.type;

import lombok.Data;

import java.util.HashMap;


@Data
public class Declarator {

    public static int POINTER = 0;
    public static int ARRAY = 1;
    public static int FUNCTION = 2;

    private int type;//类型是上面三个之一

    //数组初始化时定义的元素个数
    private int numOfElements;

    //数组元素
    Object[] elements;

    public Declarator(int type) {
        this.type = type;
    }

    public void setElementNum(int num){
        if (num < 0) {

        } else {
            numOfElements = num;
            elements = new Object[numOfElements];
        }
    }

    public void setElement(int idx,Object obj){
        checkIdx(idx);
        elements[idx] = obj;
    }

    public Object getElement(int idx){
        checkIdx(idx);
        return elements[idx];
    }

    private void checkIdx(int idx){
        if (elements == null) {
            throw new RuntimeException("This is not an Array!");
        }
        if(idx<0 || idx>=elements.length){
            throw new RuntimeException("Array Out Of Range");
        }
    }

}
